package com.xinqi.service.impl;

import com.xinqi.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author xin麒
 * @date 2023/8/10 20:11
 */
@Slf4j
@Service
public class HelloServiceImpl implements HelloService {


    @Resource
    ThreadPoolTaskExecutor taskExecutor;


    @Override
    public Object processForCount2(Long count) {
        for (int i = 0; i < 50; i++) {
            int index = i;
            taskExecutor.execute(() -> {
                try {
                    Thread.sleep(1000 * 90);
                    log.debug(String.valueOf(index));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        taskExecutor.execute(() -> {
            try {
                Thread.sleep(1000 * 90);
                log.debug(String.valueOf(50));//第50个
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return "nice";
    }

    @Override
    public Object processForCount2_2(Long count) {
        taskExecutor.execute(() -> {
            try {
                Thread.sleep(1000 * 90);
                log.debug(String.valueOf(1));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        log.debug("processForCount2_2 execute second time....");


        taskExecutor.execute(() -> {
            try {
                Thread.sleep(1000 * 90);
                log.debug(String.valueOf(50));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        log.debug("after procession.....");
        return "nice";
    }

    @Override
    public Object processForCount3(Long count) {
        for (int i = 0; i < 50; i++) {
            int index = i;
            try {
                taskExecutor.execute(() -> {
                    try {
                        Thread.sleep(1000 * 90);
                        log.debug(String.valueOf(index));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            taskExecutor.execute(() -> {
                try {
                    Thread.sleep(1000 * 90);
                    log.debug(String.valueOf(50));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "nice";
    }

    @Override
    public Object processForCount3_2(Long count) {
        try {
            taskExecutor.execute(() -> {
                try {
                    Thread.sleep(1000 * 90);
                    log.debug(String.valueOf(1));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.debug("processForCount3_2 execute second time ....");

        try {
            taskExecutor.execute(() -> {
                try {
                    Thread.sleep(1000 * 90);
                    log.debug(String.valueOf(50));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.debug("after procession.....");
        return "nice";
    }


}
